﻿using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace Level4
{
    /// <summary>
    /// 用于 多语言.
    /// </summary>
    public static class Lang
    {   
        //是否载入完毕.
        private static bool isLoaded;

        public static bool IsLoaded
        {
            get { return isLoaded; }
        }

        //字符存储字典.
        static Dictionary<string, string> langDict = new Dictionary<string, string>();
        /// <summary>
        /// 从自己数组中载入语言配置.
        /// </summary>
        /// <param name="bytes"></param>
        public static void Load(byte[] bytes)
        {
            MemoryStream ms = new MemoryStream(bytes);
            Load(ms);
        }
        /// <summary>
        /// 从文件,载入 语言配置
        /// </summary>
        /// <param name="url"></param>
        public static void Load(string filePath)
        {
            FileStream fs = new FileStream(filePath, FileMode.Open);
            Load(fs);
        }
        /// <summary>
        /// 清空语言数据字典,并从流中载入语言数据.
        /// </summary>
        /// <param name="textBytes"></param>
        public static void Load(Stream stream)
        {
            if (langDict != null)
            {
                langDict.Clear();
                isLoaded = false;
            }
            StreamReader sr = new StreamReader(stream);
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                if (string.IsNullOrEmpty(line) || line.StartsWith("//"))
                    continue;

                string[] keyValue = line.Split('=');
                string key = keyValue[0].Trim();
                string value = keyValue[1].Trim();
                langDict.Add(key, value);
            }
            isLoaded = true;
            sr.Close();
            stream.Close();
        }
        /// <summary>
        /// 根据key获取value.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Get(string key)
        {
            if (langDict.ContainsKey(key))
            {
                return langDict[key].ToString();
            }
            
            return key;
        }

        
    }

}